package com.ruoyi.qianhuankuan.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.qianhuankuan.mapper.QianhuankuanMapper;
import com.ruoyi.qianhuankuan.domain.Qianhuankuan;
import com.ruoyi.qianhuankuan.service.IQianhuankuanService;

import javax.annotation.Resource;

/**
 * 欠还款Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-25
 */
@Service
public class QianhuankuanServiceImpl implements IQianhuankuanService
{
    @Autowired
    @Resource
    private QianhuankuanMapper qianhuankuanMapper;

    /**
     * 查询欠还款
     *
     * @param id 欠还款主键
     * @return 欠还款
     */
    @Override
    public Qianhuankuan selectQianhuankuanById(Long id)
    {
        return qianhuankuanMapper.selectQianhuankuanById(id);
    }

    /**
     * 查询欠还款列表
     *
     * @param qianhuankuan 欠还款
     * @return 欠还款
     */
    @Override
    public List<Qianhuankuan> selectQianhuankuanList(Qianhuankuan qianhuankuan)
    {
        return qianhuankuanMapper.selectQianhuankuanList(qianhuankuan);
    }

    /**
     * 新增欠还款
     *
     * @param qianhuankuan 欠还款
     * @return 结果
     */
    @Override
    public int insertQianhuankuan(Qianhuankuan qianhuankuan)
    {
        return qianhuankuanMapper.insertQianhuankuan(qianhuankuan);
    }

    /**
     * 修改欠还款
     *
     * @param qianhuankuan 欠还款
     * @return 结果
     */
    @Override
    public int updateQianhuankuan(Qianhuankuan qianhuankuan)
    {
        return qianhuankuanMapper.updateQianhuankuan(qianhuankuan);
    }

    /**
     * 批量删除欠还款
     *
     * @param ids 需要删除的欠还款主键
     * @return 结果
     */
    @Override
    public int deleteQianhuankuanByIds(Long[] ids)
    {
        return qianhuankuanMapper.deleteQianhuankuanByIds(ids);
    }

    /**
     * 删除欠还款信息
     *
     * @param id 欠还款主键
     * @return 结果
     */
    @Override
    public int deleteQianhuankuanById(Long id)
    {
        return qianhuankuanMapper.deleteQianhuankuanById(id);
    }

    /**
     * 查询客户欠款列表
     *
     * @param pay 客户欠款
     * @param quanxian 权限
     * @return 客户欠款集合
     */
    @Override
    public double selectQianhuankuanList4PayAndQuanXian(String pay, String quanxian, String cangku,String today) {
        List<Qianhuankuan> list = qianhuankuanMapper.selectQianhuankuanListAll(today);
        double all = 0.0;
        if (quanxian.equals("1")) {
            if (pay.equals("未付")) {
                for (Qianhuankuan k:list) {
                    if (k.getZongqiankuanjine().toString().contains("-")) {
                        all+=Double.parseDouble(k.getZongqiankuanjine().toString().replace("-",""));
                    }
                }
                return all;
            } else if (pay.equals("已付")) {
                for (Qianhuankuan k:list) {
                    if (!k.getZongqiankuanjine().toString().contains("-")) {
                        all+=k.getZongqiankuanjine().doubleValue();
                    }
                }
                return all;
            }
        }else {
            if (pay.equals("未付")) {
                for (Qianhuankuan k:list) {
                    //getKehuName改成仓库名字段
                    if (k.getZongqiankuanjine().toString().contains("-") && k.getCangku().equals(cangku)) {
                        all+=Double.parseDouble(k.getZongqiankuanjine().toString().replace("-",""));
                    }
                }
                return all;
            } else if (pay.equals("已付")) {
                for (Qianhuankuan k:list) {
                    if (!k.getZongqiankuanjine().toString().contains("-") && k.getCangku().equals(cangku)) {
                        all+=k.getZongqiankuanjine().doubleValue();
                    }
                }
                return all;
            }
        }
        return 0;
    }

}
